//选择主键
function getMultiSelect() {
	var row = $("#tblUser").bootstrapTable('getSelections');
	if(row.length > 0) {
		return row[0].buildlogid;

	} else {
		alert("必须要选择一条信息");
		return;
	}
}

$(function() {

	//数据页面加载（主要是表格）
	$.ajax({
		type: "get", //提交类型
		/*dataType: "json", //数据类型*/
		url: "http://localhost:8082/ma/user", //数据访问路径(后端接口)
		data: {
			oper: 5
		},
		success: function(reslt) { //返回成功后所调用得函数（reslt：后端返回的数据）
			var data = JSON.parse(reslt); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
			console.log(data.results); //打印后端返回的数据到控制台上
			createTable(data.results); //将返回的数据加载到列表中
		},
		error: function(date) { //返回失败所调用得函数（date：后端返回的数据）
			console.log(date); //打印后端返回的数据
		}
	});

	// 新增按钮点击事件
	$("#btnxinz1").click(function() {
		$("#divData").modal();
	});
	//查询按钮点击事件
	$("#btnchaxun1").click(function() {
		var projectprincipal = $("#txtcx").val(); //搜取条件从页面中获取的值
		console.log("项目负责人值"+projectprincipal);
		select2(projectprincipal);
		
	});
	//获取输入内容
	$("#btnbaocun1").click(function() {

		var buildingsite = $("#buildingsite").val();
		var projectprincipal = $("#projectprincipal").val();
		insert(buildingsite,projectprincipal); //添加方法
		$("#divData").modal('hide');

	});
	//删除按钮点击事件
	$("#btndel1").click(function() {
		var x = confirm("确定删除？")
		if(x == true) {
			var buildlogid = getMultiSelect();
			console.log(buildlogid);
			DelData("4", buildlogid);
		} else {
			return;
		}

		location.reload();

	});
	//修改按钮点击事件
	//修改点击事件
	$("#btnxiugai1").click(function() {
		var row = getSelect();
		console.log("获取主键id是：" + row[0].buildlogid);
		$.ajax({
			type: "get",
			url: "http://localhost:8082/ma/user",
			data: {
				oper: 6,
				buildlogid: row[0].buildlogid
			},
			success: function(response) {
				console.log(response);
				var data = JSON.parse(response); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
				$.each(data.results, function(indexs, values) {
					console.log(values["buildingsite"] + "---" + indexs);
					$("#buildlogid2").val(values["buildlogid"]);
					$("#buildingsite1").val(values["buildingsite"]);
					$("#projectprincipal1").val(values["projectprincipal"]);
				});

				$("#divData2").modal();
			},
			error: function(response) {
				alert("修改失败");
			}
		});

		/*$("#buildlogid").val(row[0].buildlogid);
		$("#buildingsite").val(row[0].buildingsite);*/
	});
	$("#btnbaocun2").click(function() {

		var buildlogid = $("#buildlogid2").val();
		var buildingsite = $("#buildingsite1").val();
		var projectprincipal = $("#projectprincipal1").val();
		/*var begintime = $("#begintime").val();
		var endtime = $("#endtime").val();
		var projectprincipal = $("#projectprincipal").val();
		var price = $("#price").val();
		var constructionorganization = $("#constructionorganization").val();
		var constructionorganizationcontacts = $("#constructionorganizationcontacts").val();
		var accessory = $("#accessory").val();*/

		var row = getSelect();
		update(buildlogid, buildingsite,projectprincipal);
		$("#divData2").modal('hide');
	});
});

//读取表格方法
/*将数据绑定在表格上
 
 * 参数说明：userData  【必须符合Json数据结构】*/
function createTable(userData) {
	console.log("绑定的数据" + userData);
	$("#tblUser").bootstrapTable("destroy"); //清空表格  【跟后台传出的数据保持一致】

	$("#tblUser").bootstrapTable({ //给表格设置属性

		data: userData, //数据绑定的属性
		loadMsg: "正在加载数据", //
		sidePagination: "client", //客户端分页
		pageSize: "4", //每页数据的条数
		pagination: true, //是否启动分页
		/*	singleSelect:true,*/ //是否启动排序
		columns: [ //列表的字段绑定
			{
				field: "state",
				checkbox: true,
				align: "center"
			},
			{
				field: "buildingsite",
				title: "大楼地点",
				align: "center"
			},
			{
				field: "begintime",
				title: "开工时间",
				align: "center"
			},
			{
				field: "endtime",
				title: "竣工时间",
				align: "center"
			},
			{
				field: "projectprincipal",
				title: "项目负责人",
				align: "center"
			},
			{
				field: "price",
				title: "总金额",
				align: "center"
			},
			{
				field: "constructionorganization",
				title: "施工单位",
				align: "center"
			},
			{
				field: "constructionorganizationcontacts",
				title: "施工单位联系人",
				align: "center"
			},
			{
				field: "accessory",
				title: "附件",
				align: "center"
			},
			{
				field: "replenishaccessory",
				title: "补充附件",
				align: "center"
			},
			{
				field: "replenishreason",
				title: "补充理由",
				align: "center"
			},
			{
				field: "replenishtime",
				title: "补充时间",
				align: "center"
			},
			{
				field: "agreetime",
				title: "认可时间",
				align: "center"
			},
			{
				field: "lastendtime",
				title: "最终竣工时间",
				align: "center"
			},
			{
				field: "changemoney",
				title: "总金额变更",
				align: "center"
			},
			{
				field: "agreedoc",
				title: "提供双方签字内容电子版",
				align: "center"
			},
			{
				field: "isagree",
				title: "双方是否认可",
				align: "center"
			},
			{
				field: "changereason",
				title: "变更原因",
				align: "center"
			},
			{
				field: "safepollingdoc",
				title: "安全巡检报告",
				align: "center"
			},
			{
				field: "changeendtime",
				title: "变更后竣工时间",
				align: "center"
			},
			{
				field: "ischange",
				title: "是否变更",
				align: "center"
			},
			{
				field: "checker",
				title: "验收人",
				align: "center"
			},
			{
				field: "checkmaterials",
				title: "验收材料",
				align: "center"
			},
			{
				field: "checktime",
				title: "验收时间",
				align: "center"
			}
		]
	});
}
//添加数据方法
function insert(buildingsite, projectprincipal /*begintime,endtime,projectprincipal,price,constructionorganization,constructionorganizationcontacts,accessory*/ ) {
	$.ajax({
		type: "get",
		url: "http://localhost:8082/ma/user",
		data: {
			oper: 2,
			buildingsite: buildingsite,
			projectprincipal: projectprincipal
			/*begintime:begintime,
			endtime:endtime,
			projectprincipal:projectprincipal,
			price:price,
			constructionorganization:constructionorganization,
			constructionorganizationcontacts:constructionorganizationcontacts,
			accessory:accessory*/
		},
		success: function(response) {
			console.log(response);
			var data = JSON.parse(response);
			alert("添加成功");
			window.location.href = "dl.html";
		},
		error: function(response) {
			alert("添加失败");
		}
	});

}

//按项目负责人查询
function select2(projectprincipal) {
	console.log(projectprincipal);
	$.ajax({
		type: "get",
		//				dataTyp: "json", //数据类型
		url: "http://localhost:8082/ma/user",
		data: {
			oper: 1,
			projectprincipal: projectprincipal
		},
		success: function(response) {
			var data = JSON.parse(response); //将传过来的数据转换为json对象（传进来的参数是json字符串而不是json对象）
			console.log(data.results); //打印后端返回的数据到控制台上
			createTable(data.results); //将返回的数据加载到列表中
		}
	});

}

/*
 删除方法
 * */

function DelData(oper, buildlogid) {
	$.ajax({
		type: "get",
		url: "http://localhost:8082/ma/user",
		data: {
			oper: oper,
			buildlogid: buildlogid
		},
		success: function(response) {
			console.log(response);
			createTable(response);
			// console.log(response);
			// var data = JSON.parse(response);
			alert("删除成功");
		},
		error: function(response) {
			alert("删除成功");
		}
	});
}

//选择修改
function getSelect() {
	//获取列表的行
	var row = $("#tblUser").bootstrapTable('getSelections');
	//alart(row)
	if(row.length > 0) {
		//$("#divData2").modal();
		return row;
	} else {
		alert("请选择要修改的数据！");
	}
}
//修改方法
function update(buildlogid, buildingsite, projectprincipal) {
	$.ajax({
		type: "get",
		url: "http://localhost:8082/ma/user",
		data: {
			oper: 3,
			buildlogid: buildlogid,
			buildingsite: buildingsite,
			projectprincipal: projectprincipal
		},
		success: function(response) {
			console.log(response);//打印返回结果
			
			alert("修改成功");
			window.location.href="dl.html";
		},
		error: function(response) {
			alert("修改失败");
		}

	});
}
//function resetTable() {
//	document.getElementById("xinzengform").reset();
//}